//资源管理器
let TABLE = {
    actor: {
        path: "actor",
        type: cc.Prefab,
        list: ["enemy301", "enemy302", "enemy303", "enemy304", "enemy305"],
    },
    bullet: {
        path: "bullet",
        type: cc.Prefab,
        list: ["bullet1", "bullet2ios", "bullet6"],
    },
    ui: {
        path: "ui",
        type: cc.Prefab,
        list: ["panelSkillSelect"]
    },
    icon: {
        path: "icon",
        type: cc.SpriteAtlas,
        list: ["skillIcon"]
    },
    buff: {
        path: "buff",
        type: cc.Prefab,
        list: ["poisonBuff", "fireBuff"]
    },
    audio: {
        path: "audio",
        type: cc.AudioClip,
        list: []
    },
    prefab: {
        path: "prefab",
        type: cc.Prefab,
        list: []
    },
    table: {
        path: "table",
        type: cc.JsonAsset,
        list: []
    }
};

class CRes {
    constructor() {
        this.data = {};
        this.loadNum = 0;
        this.loadAllNum = Object.keys(TABLE).length;
    }
    getLoadAllNum() {
        return this.loadAllNum;
    }
    getLoadNum() {
        return this.loadNum;
    }
    doLoad() {
        for (let key in TABLE) {
            this.load(key);
        }

    }
    load(key) {
        this.data[key] = {};
        let info = TABLE[key];
        if (info.list.length) {
            this.loadRes(key);
        } else {
            this.loadResDir(key);
        }
    }
    //加载数组为空直接加载目录下所有资源
    loadResDir(key) {
        let info = TABLE[key];
        cc.loader.loadResDir(
            info.path,
            info.type,
            (err, resList) => {
                if (err) {
                    cc.log(err);
                    return;
                } else {
                    this.loadNum++;
                }
                for (let res of resList) {
                    this.data[key][res.name] = res;
                }
            }
        )
    }
    //根据list进行加载
    loadRes(key) {
        this.data[key] = {};
        let info = TABLE[key];
        let fileList = [];
        for (let fName of info.list) {
            fileList.push(info.path + '/' + fName);
        }
        let func = () => {
            let tempList = fileList.splice(0, 5);
            cc.loader.loadResArray(
                tempList,
                info.type,
                (err, resList) => {
                    if (fileList.length > 0) {
                        func();
                    }
                    if (err) {
                        cc.log(err);
                        return;
                    }
                    for (let res of resList) {
                        this.data[key][res.name] = res;
                    }
                    this.loadNum++;
                }
            );
        }
        func();
    }
    //通过键值和资源名获取资源
    getRes(key, name) {
        if (this.data[key] && this.data[key][name]) {
            return this.data[key][name];
        }
        return null;
    }
};
window.g_Res = new CRes;